
#include "ILibrary.h"

using namespace BWE;

ILibrary::ILibrary(const char* name)
{
	this->name = name;
}

bool ILibrary::empty() const
{
	return variants.empty() && functionMap.empty();
}

void ILibrary::registFunction(const BString& declare, Function function)
{
	if (declare.empty())
		return;

	if (function == 0)
		return;

	BString name = declare;

	FuncInfoHolder finfo = new FuncInfo();
	if (!AnalyseDeclare(name, finfo->type, finfo->params))
		return;

	if (functionMap.contain(name))
		return;

	finfo->cfunc = function;
	functionMap[name] = finfo;
}

void ILibrary::registVariant(const char* type, const char* name, const BValue& value)
{
	if (!type && !name)
		return;
	for (int i = 0; i < variants.size(); i++)
	{
		const VariantInfo& vinfo = variants[i];
		if (vinfo.name == name)
			return;
	}
	VariantInfo& vinfo = variants.append();
	vinfo.type = type;
	if (vinfo.type.beginWith("const "))
	{
		vinfo.cnst = true;
		vinfo.type.remove("const ");
	}
	vinfo.name = name;
	vinfo.value = value;
}
